package lc;

public class Ex71_2 {
    class Solution {
        public String simplifyPath(String path) {
            String[] ss = path.split("/");
            int n = ss.length, idx = -1;
            String[] stack = new String[n];
            // System.out.println(Arrays.toString(ss));
            // System.out.println(ss[0] == "");
            for (int i = 0; i < n; i++) { 
                if ((!"".equals(ss[i])) && (!".".equals(ss[i]))) {
                    if ("..".equals(ss[i])) {
                        if (idx >= 0) {
                            idx--;
                        }
                    } else {
                        stack[++idx] = ss[i];
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            while (idx >= 0) {
                sb.insert(0, "/" + stack[idx--]);
            }
            if (sb.length() == 0) {
                sb.append("/");
            }
            return sb.toString();
        }
    }
}
